#include "resources.h"
#include "sound/GameSounds.h"
#include "commentdef.h"
#include "QDebug"
#include "scene/gamescene.h"
#include "scene/splashscene.h"
#include "view/mainwindow.h"
#include <QSettings>
#include "item/imageitem.h"
#include <QLocale>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include <QApplication>
#include "scene/mainscene.h"
#include "scene/selectscene.h"
#include <QImage>
#include <QPixmap>
#include "scene/helpview.h"

splashScene* splashscene(NULL);
MainScene*  mainscene(NULL);
SelectScene* selscene(NULL);
GameScene*  gamescene(NULL);
HelpView*   helpview(NULL);

MainWindow* resources::m_mainwindow = NULL;
GameScene* resources::m_gamescene = gamescene;

QImage* resources::m_mainExitImage = NULL;
QImage* resources::m_mainExitpImage = NULL;
QImage* resources::m_mainHelpImage = NULL;
QImage* resources::m_mainHelppImage = NULL;
QImage* resources::m_mainStartImage = NULL;
QImage* resources::m_mainStartpImage = NULL;
QImage* resources::m_mainSoundImage = NULL;
QImage* resources::m_mainSoundsImage = NULL;

int resources::m_iLanguage = 0;

bool resources::m_bplaysound = false;

QImage* resources::m_selBackImage = NULL;
QImage* resources::m_selBackpImage = NULL;
QImage* resources::m_selFirstImage = NULL;
QImage* resources::m_selFirstpImage = NULL;
QImage* resources::m_selSecondImage = NULL;
QImage* resources::m_selSecondpImage = NULL;
QImage* resources::m_selThirdImage = NULL;
QImage* resources::m_selThirdpImage = NULL;

int resources::m_icurlevel = 0;

QPixmap*    resources::m_gamebgImage[3] = {NULL,NULL,NULL};
QImage* resources::m_gameball = NULL;

QImage* resources::m_helpaboutImage = NULL;
QImage* resources::m_helphelpImage = NULL;
QImage* resources::m_helpbackImage = NULL;

QImage* resources::m_pauseBgImage = NULL;
QImage* resources::m_pauseContinueImage = NULL;
QImage* resources::m_pauseContinuePImage = NULL;
QImage* resources::m_pauseMainImage = NULL;
QImage* resources::m_pauseMainPImage = NULL;
QImage* resources::m_pauseNextImage = NULL;
QImage* resources::m_pauseNextPImage = NULL;
QImage* resources::m_pauseNewImage = NULL;
QImage* resources::m_pauseNewPImage = NULL;
QImage* resources::m_pauseSureImage = NULL;
QImage* resources::m_pauseSurePImage = NULL;
QImage* resources::m_pauseRestartImage = NULL;
QImage* resources::m_pauseRestartPImage = NULL;

GameSounds* resources::m_sound = NULL;
resources::resources()
{

}

void resources::SetSplash()
{
    if(splashscene == NULL)
    {
        splashscene = new splashScene(m_mainwindow);
        m_mainwindow->setScene(splashscene);
    }
}

void resources::LoadResource()
{
    m_mainExitImage= new QImage(":/image/main/menu_exit_n.png");
    m_mainExitpImage = new QImage(":/image/main/menu_exit_p.png");
    m_mainHelpImage = new QImage(":/image/main/menu_help_n.png");
    m_mainHelppImage = new QImage(":/image/main/menu_help_p.png");
    m_mainStartImage = new QImage(":/image/main/menu_start_n.png");
    m_mainStartpImage = new QImage(":/image/main/menu_start_p.png");
    m_mainSoundImage = new QImage(":/image/main/menu_fx_on.png");
    m_mainSoundsImage = new QImage(":/image/main/menu_fx_off.png");

    m_mainExitpImage = new QImage(":/image/main/menu_exit_p.png");
    m_mainHelpImage = new QImage(":/image/main/menu_help_n.png");
    m_mainHelppImage = new QImage(":/image/main/menu_help_p.png");
    m_mainStartImage = new QImage(":/image/main/menu_start_n.png");
    m_mainStartpImage = new QImage(":/image/main/menu_start_p.png");
    m_mainSoundImage = new QImage(":/image/main/menu_fx_on.png");
    m_mainSoundsImage = new QImage(":/image/main/menu_fx_off.png");

    m_selFirstImage = new QImage(":/image/sel/start_easy_n.png");
    m_selFirstpImage = new QImage(":/image/sel/start_easy_p.png");
    m_selSecondImage = new QImage(":/image/sel/start_nomal_n.png");
    m_selSecondpImage = new QImage(":/image/sel/start_nomal_p.png");
    m_selBackImage = new QImage(":/image/sel/back_n.png");
    m_selBackpImage = new QImage(":/image/sel/back_p.png");
    m_selThirdImage = new QImage(":/image/sel/start1_417,57.png");
    m_selThirdpImage = new QImage(":/image/sel/start222.png");

    QString str;
    for(int i = 0; i < 3; ++i)
    {
        str = QString(":/image/game/stage%1_bg.jpg").arg(i + 1);
        m_gamebgImage[i] = new QPixmap(str);
    }
    m_gameball = new QImage(":/image/game/ball.png");

    m_helpaboutImage = new QImage(":/image/help/about.png");
    m_helphelpImage = new QImage(":/image/help/help.png");
    m_helpbackImage = new QImage(":/image/help/menu_back_p.png");

    m_pauseBgImage = new QImage(":/image/pause/bg.png");
    m_pauseContinueImage = new QImage(":/image/pause/pauce_conture.png");
    m_pauseContinuePImage = new QImage(":/image/pause/pauce_conture_p.png");
    m_pauseMainImage = new QImage(":/image/pause/over_menu.png");
    m_pauseMainPImage = new QImage(":/image/pause/over_menu_p.png");
    m_pauseNextImage = new QImage(":/image/pause/win_next.png");
    m_pauseNextPImage = new QImage(":/image/pause/win_next_p.png");
    m_pauseNewImage = new QImage(":/image/pause/win_new.png");
    m_pauseNewPImage = new QImage(":/image/pause/win_new_p.png");
    m_pauseSureImage = new QImage(":/image/pause/win_ok.png");
    m_pauseSurePImage = new QImage(":/image/pause/win_ok_p.png");
    m_pauseRestartImage = new QImage(":/image/pause/over_replay.png");
    m_pauseRestartPImage = new QImage(":/image/pause/over_replay_p.png");

    mainscene = new MainScene(m_mainwindow);
    selscene = new SelectScene(m_mainwindow);
    gamescene = new GameScene(m_mainwindow);
    m_gamescene = gamescene;
    helpview = new HelpView(m_mainwindow);
}

void resources::LoadData()
{
    QSettings settings("MySoft", "Score");
    m_bplaysound = settings.value("main/sound",false).toBool();
}

void resources::SaveData()
{
    QSettings settings("MySoft", "Score");
    settings.setValue("main/sound",m_bplaysound);
}

void resources::SetMain()
{
    static bool bfirst = true;
    if(bfirst)
    {
        bfirst = false;
    }
    else
    {
        m_sound->stop(0);
        m_sound->stop(1);
        if(m_bplaysound)
        {
            m_sound->playBgSound(0);
        }
    }

    m_mainwindow->m_timer.setInterval(100);
    m_mainwindow->setScene(mainscene);
    if(splashscene != NULL)
    {
        delete splashscene;
        splashscene = NULL;
    }
}

void resources::SetSel()
{  
    m_mainwindow->setScene(selscene);
}

void resources::SetGame()
{
    m_sound->stop(0);
    m_sound->stop(1);
    if(m_bplaysound)
    {
        m_sound->playBgSound(1);
    }
    m_mainwindow->m_timer.setInterval(40);
    gamescene->initGame(resources::m_icurlevel,0);
    m_mainwindow->setScene(gamescene);
}

void resources::SetHelp(bool bhelp)
{
    helpview->IsShowHelp(bhelp);
    m_mainwindow->setScene(helpview);
}

void resources::WindowRenderStart()
{
    m_mainwindow->m_timer.start();
}
